﻿
Public Class FrmCategoria
    Dim classController As New Class_RC_Controller.rc_Empleados
    Private _codCategoria As Integer = 0

    Public Property codCategoria() As Integer
        Get
            Return _codCategoria
        End Get
        Set(ByVal value As Integer)
            _codCategoria = value
        End Set
    End Property
    Private Sub FrmSector_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.llenarcbotipoemp()
        Me.CargarDatos()
    End Sub

    Private Sub CargarDatos()
        Try
            If codCategoria > 0 Then

                Dim ds As New DataTable
                ds = classController.get_Categoria_All(oiml.dbConn, codCategoria, "", 0)

                If Not ds Is Nothing Then
                    txtcodigo.Text = ds.Rows(0)("CodCategoria")
                    txtdescripcion.Text = ds.Rows(0)("DescCategoria")
                    txtjornal.Text = ds.Rows(0)("JornalBase")
                    txthoras.Text = ds.Rows(0)("HorasTrab")
                    btneliminar.Enabled = True
                    cmbtipoemp.SelectedValue = ds.Rows(0)("CodTipoEmp").ToString
                End If
            Else
                btneliminar.Enabled = False
                txtdescripcion.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub llenarcbotipoemp()

        Dim dstipoemp As DataTable
        dstipoemp = classController.get_tipo_Emp(oiml.dbConn, cmbtipoemp.SelectedValue)
        With Me.cmbtipoemp
            .DataSource = dstipoemp
            .DisplayMember = "DescTipoEmp"
            .ValueMember = "CodTipoEmp"
        End With


    End Sub
    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtDescripcion.Text = "" Then
            ErrorProvider.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtDescripcion, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
        If ValidarCampos() Then
            Exit Sub
        End If
        Dim salxhora As Decimal
        salxhora = (txthoras.Text / txtjornal.Text)

        If codCategoria = 0 Then
            codCategoria = classController.Insert_Categoria(oiml.dbConn, txtdescripcion.Text, cmbtipoemp.SelectedValue, txthoras.Text, txtjornal.Text, salxhora)
            If codCategoria > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Categoria", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Categoria", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_Categoria(oiml.dbConn, codCategoria, txtdescripcion.Text, cmbtipoemp.SelectedValue, txthoras.Text, txtjornal.Text, salxhora) Then
                MessageBox.Show("Registro grabado con exito!", "Categoria", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Categoria", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If
    End Sub

    Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneliminar.Click
        If codCategoria > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Categoria", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_Categoria(oiml.dbConn, codCategoria) Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Categoria", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If
    End Sub

    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancelar.Click
        Me.Close()
    End Sub
End Class